
// @koala-prepend "../../libs/touch-list.js"

$(function(){
    var isLogin = urls.checklogin();
    if(!isLogin) {
        $('.alert-hint').html('未登录，请先登录');
        $('.btn-cancel').hide();
        $('.btn-alert-sure').click(function(){
            location.href = '../login';            
        })
        $('.modal-alert').modal('show');
        setTimeout(function(){
            location.href = '../login';
        }, 3000);
        return;
    }
    Page.token = urls.GetQueryString('t');
    $('.btn-add-moto').attr('href', '../modify?t=' + Page.token);
    $('.btn-manage-merchants').attr('href', '../merchants?t=' + Page.token);
	Page.search();
});

var Page = {};
Page.search = function() {
	var url = urls.server + '/bikes';

	$.ajax({
        type: 'GET',
        async: true,
        url: url,
        dataType: 'json',
        success: function(e){
        	if(e.length > 0) {
        		Page.showData(e);
        	}
        	else {
        		$('.bikes-data-list').html('<div class="error">未查询到数据</div>');
        	}
        }
    });
}

Page.showData = function(datas) {
	var h = '';
	for( var i=0, len=datas.length; i<len; i++ ) {
		var item = datas[i];

		h += '<div class="line-wrapper" data-id="'+ item.id +'">';
		h += '	<div class="line-scroll-wrapper">';
		h += '		<div class="line-normal-wrapper">';
        // h += '          <a href="../zongshu?key='+item.id+'&t='+ Page.token +'"><div class="title">'+ item.name +'</div>';
		h += '			<a href="javascript:void(0);"><div class="title">'+ item.name +'</div>';
		h += '			<div class="param">';
		h += '				<span class="lbl">编号：</span>';
		h += '				<span class="text">'+ item.number +'</span>';
		h += '			</div>';
		// h += '			<div class="param">';
		// h += '				<span class="price">'+ (item.price/10000) +'万</span>';
		// h += '			</div>';
		// h += '			<div class="param">';
		// h += '				<span class="lbl">'+ item.year + '/' + item.real_km +'</span>';
		// h += '			</div>';
		h += '		  </a></div>';
		h += '		<div class="line-function">';
		h += '			<a class="btn btn-edit" href="../modify?key='+ item.id +'&t='+ Page.token +'">编辑</a>';
		h += '			<a class="btn btn-imgs" href="../tupian?key='+ item.id +'&t='+ Page.token +'">图片管理</a>';
		h += '			<a class="btn btn-delete" data-id="'+ item.id +'" data-name="'+ item.name +'" href="javascript:;">删除</a>';
		h += '		</div>';
		h += '	</div>';
		h += '</div>';
	}

	h += '<div class="error">没有更多数据了</div>'

	$('.bikes-data-list').html(h);

	setLiLeftSliding(200);

    $('.btn-delete').click(Page.openRmoveModal);
}

Page.openRmoveModal = function(){
    var me = $(this);
    $('.alert-hint').html('确定删除车辆：' + me.attr('data-name') + ' ?删除后不可恢复');
    $('.btn-alert-sure').unbind('click').click(Page.remove).attr({'data-id': me.attr('data-id')});
    $('.modal-alert').modal('show');
}
Page.remove = function() {
    var me = $(this);
    var url = urls.server + '/bikes/' + me.attr('data-id');

    $.ajax({
        type: 'delete',
        async: true,
        url: url,
        data: {
            access_token: 'AABBCCDD'
        },
        dataType: 'json',
        success: function(e){
            if(e && e.action == 'success') {
                $('.modal-alert').modal('hide');
                $('.line-wrapper[data-id="'+ me.attr('data-id') +'"]').remove();
            }
        }
    });
}


